# 开启日志
from utils.log import *

fore = {
    "default": 0,
    "black": 30,
    "red": 31,
    "green": 32,
    "yellow": 33,
    "blue": 34,
    "purple": 35,
    "cyan": 36,
    "white": 37,
    "light_black": 90,
    "light_red": 91,
    "light_green": 92,
    "light_yellow": 93,
    "light_blue": 94,
    "light_purple": 95,
    "light_cyan": 96,
    "light_white": 97,
}

back = {
    "default": 0,
    "black": 40,
    "red": 41,
    "green": 42,
    "yellow": 43,
    "blue": 44,
    "purple": 45,
    "cyan": 46,
    "white": 47,
    "light_black": 100,
    "light_red": 101,
    "light_green": 102,
    "light_yellow": 103,
    "light_blue": 104,
    "light_purple": 105,
    "light_cyan": 106,
    "light_white": 107,
    "bold_black": 100,
    "bold_red": 101,
    "bold_green": 102,
    "bold_yellow": 103,
    "bold_blue": 104,
    "bold_purple": 105,
    "bold_cyan": 106,
    "bold_white": 107,
}


def esc_code(_dict: dict):
    _code_dict = {}
    for _key, _value in _dict.items():
        _code = esc(_value)
        _code_dict.setdefault(_key, _code)
    return _code_dict


foreground = esc_code(fore)

# background = esc_code(back)


default = foreground["default"]
red = foreground["light_red"]
green = foreground["light_green"]
yellow = foreground["light_yellow"]
blue = foreground["light_blue"]
purple = foreground["light_purple"]
cyan = foreground["light_cyan"]
white = foreground["light_white"]

#  是否输出日志
key = True
# 日志等级
leve = logging.NOTSET
# 只输出一个等级的日志
only = False
# 是否写入文件
write_fils = True

CRITICAL = 50  # 严重错误
ERROR = 40  # 错误
WARNING = 30  # 警告
INFO = 20  # 信息
DEBUG = 10  # 调试
NOTSET = 0  # 默认级别，不设置级别时，日志将全部记录

fore_logger = Log(log_color=Color.FORE)
back_logger = Log(log_color=Color.BACK)

logger = fore_logger


line = "==============================================="
error_line = "^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^"

def _match_logger(_color: Color = Color.FORE):
    """选择日志字体输出效果，FORE为普通字体，BACK为带背景色字体"""
    global logger
    match _color:
        case Color.FORE:
            logger = fore_logger
        case Color.BACK:
            logger = back_logger


def show_log(_mess, level=logging.DEBUG):
    """输出日志"""
    if write_fils:
        logger.log_show_store(_mess, level)
    else:
        logger.log_show(_mess, level)


def debug(_mess, _color: Color = Color.FORE):
    """logging.DEBUG，青色/黑字青底"""
    if not key:
        return

    _match_logger(_color)
    if leve == logging.DEBUG:
        show_log(_mess)
    else:
        if leve < logging.DEBUG:
            show_log(_mess)


def info(_mess, _color: Color = Color.FORE):
    """logging.INFO，绿色/黑字绿底"""
    if not key:
        return

    _match_logger(_color)
    if only:
        if leve == logging.INFO:
            # print(f"{green}{line}{default}")
            show_log(_mess, logging.INFO)
    else:
        if leve < logging.INFO:
            # print(f"{green}{line}{default}")
            show_log(_mess, logging.INFO)


def warning(_mess, _color: Color = Color.FORE):
    """logging.WARNING，黄色/黑字黄底"""
    if not key:
        return

    _match_logger(_color)
    if only:
        if leve == logging.WARNING:
            # print(f"{yellow}{line}{default}")
            show_log(_mess, logging.WARNING)
    else:
        if leve < logging.WARNING:
            # print(f"{yellow}{line}{default}")
            show_log(_mess, logging.WARNING)


def error(_mess, _color: Color = Color.FORE):
    """logging.ERROR，红色/黑字红底"""
    if not key:
        return

    _match_logger(_color)
    if only:
        if leve == logging.ERROR:
            print(f"{red}{error_line}{default}")
            show_log(_mess, logging.ERROR)
            print(f"{red}{error_line}{default}")

    else:
        print(f"{red}{error_line}{default}")
        show_log(_mess, logging.ERROR)
        print(f"{red}{error_line}{default}")


def tips(_mess, _color: Color = Color.FORE):
    """logging.CRITICAL，紫色/黑字紫底"""
    if not key:
        return

    _match_logger(_color)
    if only:
        if leve == logging.CRITICAL:
            # print(f"{purple}{error_line}{default}")
            show_log(_mess, logging.CRITICAL)
            # print(f"{purple}{error_line}{default}")
    else:
        # print(f"{purple}{error_line}{default}")
        show_log(_mess, logging.CRITICAL)
        # print(f"{purple}{error_line}{default}")


def my_mess(_color: Color = Color.FORE):
    """logging.INFO，绿色/黑字绿底"""
    if not key:
        return

    _match_logger(_color)

    def log_mess():
        stack_frames = traceback.extract_stack()
        stack_frames = stack_frames[:-2]
        for frame in stack_frames:
            show_log(f"File: {frame.filename}", logging.INFO)
            show_log(f"Line: {frame.lineno}", logging.INFO)
            show_log(f"Function: {frame.name}", logging.INFO)
            show_log(f"Code: {frame.line}", logging.INFO)
            print(f"{green}---------------------------------------------------{default}")

    if only:
        if leve == logging.INFO:
            print(f"{green}{line}{default}")
            log_mess()
    else:
        print(f"{green}{line}{default}")
        log_mess()


if __name__ == '__main__':
    print(f'####################### {__file__} #######################')
    # leve = 3
    # only = False
    # write_fils = True
    print("--------------------------")
    debug("hello")
    print("--------------------------")
    info("hello")
    print("--------------------------")
    warning('hello')
    print("--------------------------")
    error("hello")
    print("--------------------------")
    tips("hello")
    print("--------------------------")
    my_mess()

    print("--------------------------")
    debug("hello", Color.BACK)
    print("--------------------------")
    info("hello", Color.BACK)
    print("--------------------------")
    warning("hello", Color.BACK)
    print("--------------------------")
    error("hello", Color.BACK)
    print("--------------------------")
    tips("hello", Color.BACK)
    print("--------------------------")
    my_mess(Color.BACK)
